﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax {
    /// <summary>
    /// PlsettingAjax 的摘要说明
    /// </summary>
    public class PlsettingAjax : IHttpHandler {
        PlsettingController Controller = new PlsettingController();
        public void ProcessRequest(HttpContext context) {
            string type = string.Empty;
            if(context.Request["action"] != null) {
                type = context.Request["action"];

                switch(type) {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    default:
                        break;
                }
            }
        }
        private void GetList(HttpContext context) {
            List<PLSetting> list = Controller.GetList();
            string json = JsonHelper.ListToJson<PLSetting>(list);
            context.Response.Write(json);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        private void Add(HttpContext context) {
            string name = context.Request["name"];
            double overtopValue = Convert.ToDouble(context.Request["OvertopValue"]);
            double undervalue = Convert.ToDouble(context.Request["undervalue"]);
            int handcount = Convert.ToInt32(context.Request["handcount"]);
            double initvalue = Convert.ToDouble(context.Request["initvalue"]);
            double normvalue = Convert.ToDouble(context.Request["normvalue"]);
            int tunevalue = Convert.ToInt32(context.Request["tunevalue"]);
            PLSetting model = new PLSetting() {
            Name=name,
            OvertopValue=overtopValue,
            UnderValue=undervalue,
            HandCount=handcount,
            InitValue=initvalue,
            NormValue=normvalue,
            TuneValue=tunevalue
            };
            int id = Controller.Add(model);
            context.Response.Write(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        private void Update(HttpContext context) {
            int id =Convert.ToInt32(context.Request["id"]);
            string name = context.Request["name"];
            double overtopValue = Convert.ToDouble(context.Request["OvertopValue"]);
            double undervalue = Convert.ToDouble(context.Request["undervalue"]);
            int handcount = Convert.ToInt32(context.Request["handcount"]);
            double initvalue = Convert.ToDouble(context.Request["initvalue"]);
            double normvalue = Convert.ToDouble(context.Request["normvalue"]);
            int tunevalue = Convert.ToInt32(context.Request["tunevalue"]);

            PLSetting model = new PLSetting() {
                ID=id,
                Name = name,
                OvertopValue = overtopValue,
                UnderValue = undervalue,
                HandCount = handcount,
                InitValue = initvalue,
                NormValue = normvalue,
                TuneValue = tunevalue
            };
            context.Response.Write(Controller.Update(model));
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }
}